package com.cdtu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdtu.entity.CertifyType;
import com.cdtu.entity.Study;
import com.cdtu.service.StudyService;
import com.cdtu.mapper.StudyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author lft
 * @description 针对表【study】的数据库操作Service实现
 * @createDate 2023-02-06 17:15:05
 */
@Service
public class StudyServiceImpl extends ServiceImpl<StudyMapper, Study>
        implements StudyService{

    @Autowired
    private StudyMapper studyMapper;
    //查询总记录条数
    @Override
    public int getTotal() {
        return studyMapper.findTotal();
    }
    //查询所有视频资源
    @Override
    public List<Study> queryAll() {
        return  studyMapper.selectList(null);
    }
    //具备按照certifyTypeId和title同时查询，也支持单一查询，或者只分页查询
    @Override
    public List<Study> queryByPage(Integer pageNum, Integer pageSize, String title, Long certifyTypeId) {
        int startIndex = (pageNum-1)*pageSize;
        List<Study> studies = studyMapper.selectByTitleAndCertifyTypeIdAndPage(startIndex, pageSize, title, certifyTypeId);
        return studies;
    }
    //查询所有视频类型
    @Override
    public List<CertifyType> findCertifyType() {
        List<CertifyType> certifyTypeList = studyMapper.selectAllCertifyType();
        return certifyTypeList;
    }
    //根据id查询视频资源
    @Override
    public Study queryById(Long id) {
        return studyMapper.searchById(id);
    }
    //添加视频资源
    @Override
    public int saveVideosSource(Study study) {
        return studyMapper.insertVideoSource(study);
    }
    //删除视频资源
    @Override
    public int deleteVideoById(Long id) {
        return studyMapper.delByIdInt(id);
    }
    //修改视频资源,判断ItemSrc和VeidoSrc是否为空，为空则进行文件删除操作
    @Override
    public int modifyById(Study study) {
        int i;
        if (study.getVedioSrc() == null) {
            if (study.getTopicSrc() == null) {
                i = studyMapper.delByIdInt(study.getId());
                return i;
            }
        }
        i = studyMapper.updateById(study);
        return i;
    }
}




